import { Controller } from "../../mvc/core/Controller";
import { DomWarBeaconHintBtn } from "../view/dominionWar/DomWarBeaconHintBtn";
import { ResFileConst } from "../resource/ResFileConst";
import { HintBtnBase } from "../view/HintBtnBase";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { DataEvent } from "../events/DataEvent";
import { DominionId } from "../../../Message/Public/DominionId";
import { DomWarConst } from "../view/dominionWar/DomWarConst";
import { ProxyConfig } from "../resource/ProxyConfig";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SDominionBattleSituationInfos } from "../../../Message/Public/SDominionBattleSituationInfos";
import { SDominionReply } from "../../../Message/Public/SDominionReply";
import { GameProxy } from "../mvc/GameProxy";
import { SDominionInviteInfo } from "../../../Message/Public/SDominionInviteInfo";
import { SDominionDateStage } from "../../../Message/Public/SDominionDateStage";
import { UnionInviteInfo } from "../view/dominionWar/info/UnionInviteInfo";
import { UnionInvoteInfoPannel } from "../view/dominionWar/beacon/UnionInvoteInfoPannel";
import { AuctionPricePannel } from "../view/dominionWar/beacon/AuctionPricePannel";
import { AuctionRankPannel } from "../view/dominionWar/beacon/AuctionRankPannel";
import { SDominionBattlePreviewInfo } from "../../../Message/Public/SDominionBattlePreviewInfo";
import { SDominionAuctionCityInfo } from "../../../Message/Public/SDominionAuctionCityInfo";
import { SDominionCityInfo } from "../../../Message/Public/SDominionCityInfo";
import { IView } from "../../mvc/interfaces/IView";
import { DomWarBeaModule } from "../../../modules/DomWarBeaModule";
type int = number;
//class DomWarBeaconController
    
    export  class DomWarBeaconController extends Controller
    {
       

        /* internal  */get icoBtn(): DomWarBeaconHintBtn
        {
            if (!this._icoBtn) 
            {
                this._icoBtn = new DomWarBeaconHintBtn(ResFileConst.DomWarbeaconHintBtn, HintBtnBase.SHINE_CLICK);
            }
            return this._icoBtn;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.ToOpenDomWarBeacon, this.onBeaconModuleOpen);
            Dispatcher.addEventListener(EventName.ShowDomWarBeaconBtn, this.onShowBtn);
            Dispatcher.addEventListener(EventName.GetDomWarBeaPannelInfo, this.getPannelDataInfo);
            Dispatcher.addEventListener(EventName.ToAuctionCity, this.auctionCity);
            Dispatcher.addEventListener(EventName.ToOpenAuctionCityAlert, this.openAuctionCity);
            Dispatcher.addEventListener(EventName.ToOpenInviteUI, this.openInviteUI);
            Dispatcher.addEventListener(EventName.ToOpenDominionUIByIndex, this.moduleGotoTabIndex);
            Dispatcher.addEventListener(EventName.DomWarInviteServUnion, this.inviteUnion);
            Dispatcher.addEventListener(EventName.UnionInviteReply, this.inviteReply);
            Dispatcher.addEventListener(EventName.AuctionCitySuccess, this.onAuctionSuccess);
            Dispatcher.addEventListener(EventName.InviteUnionSuccess, this.onInviteSuccess);
            Dispatcher.addEventListener(EventName.UnionReplayComplete, this.onReplayComplete);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionCityInfo, this.onCityInfoUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionAuctionCityResult, this.onAuctionCityInfoUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionBattlePreviewInfo, this.onMatchInfoUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionInit, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionLightCity, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionChooseCity, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionOpenTest, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionOpenToAll, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionChooseAlliance, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionStageInfo, this.onDateStateUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionBattleInvite, this.onReceiveInvite);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionBattleReply, this.onInviteReplay);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDominionGetSituation, this.onSituationUpdate);
            return;
        }

        /* internal  */onReplayComplete(arg1: DataEvent): void
        {
            var loc3=0;
            var loc4=null;
            var loc5=0;
            var loc6=false;
            var loc7=false;
            var loc1=arg1.data;
            var loc2=new DominionId();
            loc2.proxyId = loc1.proxyId;
            loc2.serverId = loc1.serverId;
            if (this.cache.dominionBea.inviteInfo) 
            {
                loc3 = this.cache.dominionBea.inviteInfo.seqInvite.length;
                loc5 = 0;
                while (loc5 < loc3) 
                {
                    loc4 = this.cache.dominionBea.inviteInfo.seqInvite[loc5];
                    loc6 = DomWarConst.isSameServer(loc4.mainActId, loc2);
                    loc7 = DomWarConst.isSameServer(loc4.mainDefId, loc2);
                    if (loc6 || loc7) 
                    {
                        this.cache.dominionBea.inviteInfo.seqInvite.splice(loc5, 1);
                        if (this._inviteInfo && !this._inviteInfo.isHide) 
                        {
                            this._inviteInfo.update({"type": 2});
                        }
                    }
                    ++loc5;
                }
            }
            return;
        }

        /* internal  */onInviteSuccess(arg1: DataEvent): void
        {
            var loc1=ProxyConfig.instance.getProxyName(arg1.data.proxyId) + "_" + arg1.data.serverId;
            MsgManager.showRollTipsMsg(Language.getStringByParam(90673, loc1));
            return;
        }

        /* internal  */onAuctionSuccess(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getStringByParam(90674, arg1.data.price.toString()));
            return;
        }

        /* internal  */onSituationUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDominionBattleSituationInfos;
            this.cache.dominionBea.situationInfo = loc1;
            this.module.update({"data": loc1, "index": 2});
            return;
        }

        /* internal  */onInviteReplay(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=arg1.messageBase as SDominionReply;
            if (loc1) 
            {
                loc2 = "";
                if (loc1.reply != 0) 
                {
                    loc2 = Language.getString(90664);
                }
                else 
                {
                    loc2 = Language.getString(90663);
                }
                loc3 = "[" + DomWarConst.getSerNameByDomId(loc1.replyId) + "]";
                loc4 = Language.getStringByParam(90669, loc3, loc2);
                MsgManager.showRollTipsMsg(loc4);
                GameProxy.copy.getDominionMatchList();
            }
            return;
        }

        /* internal  */onReceiveInvite(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDominionInviteInfo;
            this.cache.dominionBea.inviteInfo = loc1;
            if (this._inviteInfo && !this._inviteInfo.isHide) 
            {
                this._inviteInfo.update({"type": 2});
            }
            if (loc1.seqInvite.length > 0) 
            {
                this.module.receiveInvite();
            }
            return;
        }

        /* internal  */inviteReply(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.copy.inviteReply(loc1.proxyId, loc1.serverId, loc1.x, loc1.y, loc1.reply);
            return;
        }

        /* internal  */onDateStateUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDominionDateStage;
            this.cache.dominionBea.dateState = loc1;
            if (!(this.cache.dominionBea.dateState.stage == 0) && this.icoBtn.isHide) 
            {
                this.icoBtn.show();
            }
            return;
        }

        /* internal  */inviteUnion(arg1: DataEvent): void
        {
            var loc1=arg1.data as UnionInviteInfo;
            GameProxy.copy.inviteDominionAlliance(loc1.domId.proxyId, loc1.domId.serverId, loc1.x, loc1.y);
            return;
        }

        /* internal  */moduleGotoTabIndex(arg1: DataEvent): void
        {
            this.module.showToTab(arg1.data.index as int);
            return;
        }

        /* internal  */openInviteUI(arg1: DataEvent): void
        {
            var loc1=0;
            if (cache.country.isDominionPrivilege()) 
            {
                loc1 = arg1.data.type as int;
                if (!this._inviteInfo) 
                {
                    this._inviteInfo = new UnionInvoteInfoPannel();
                }
                this._inviteInfo.show();
                this._inviteInfo.update(arg1.data);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(90659));
            }
            return;
        }

        /* internal  */openAuctionCity(arg1: DataEvent): void
        {
            if (!this._auctionWin) 
            {
                this._auctionWin = new AuctionPricePannel();
            }
            if (!this._auctionRank) 
            {
                this._auctionRank = new AuctionRankPannel();
            }
            var loc1=arg1.data.type;
            if (loc1 != 1) 
            {
                if (loc1 == 2) 
                {
                    this._auctionRank.show();
                    this._auctionRank.update(arg1.data.data);
                }
            }
            else 
            {
                this._auctionWin.show();
                this._auctionWin.update(arg1.data);
            }
            return;
        }

        /* internal  */auctionCity(arg1: DataEvent): void
        {
            var loc2=0;
            var loc1=arg1.data.price;
            if (this.cache.dominionBea.auctionCityInfo && loc1 >= this.cache.dominionBea.auctionCityInfo.lowPrice) 
            {
                loc2 = this.cache.dominionBea.auctionCityInfo.source - this.cache.dominionBea.auctionCityInfo.usedSource;
                if (loc2 >= loc1) 
                {
                    GameProxy.copy.auctionDominionCity(arg1.data.x, arg1.data.y, loc1);
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(90676));
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(90658));
            }
            return;
        }

        /* internal  */onMatchInfoUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDominionBattlePreviewInfo;
            this.module.update({"data": loc1, "index": 2});
            return;
        }

        /* internal  */onAuctionCityInfoUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDominionAuctionCityInfo;
            this.cache.dominionBea.auctionCityInfo = loc1;
            this.module.update({"data": loc1, "index": 1});
            return;
        }

        /* internal  */onCityInfoUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDominionCityInfo;
            this.module.update({"data": loc1, "index": 0});
            return;
        }

        /* internal  */getPannelDataInfo(arg1: DataEvent): void
        {
            var loc1=arg1.data.index;
            var loc2=loc1;
            switch (loc2) 
            {
                case 0:
                {
                    GameProxy.copy.getDominionCityList();
                    break;
                }
                case 1:
                {
                    GameProxy.copy.getDominionAuctionCityList();
                    break;
                }
                case 2:
                {
                    GameProxy.copy.getDominionMatchList();
                    break;
                }
                case 3:
                {
                    GameProxy.copy.getDominionSituationList();
                    break;
                }
            }
            return;
        }

        /* internal  */onShowBtn(arg1: DataEvent): void
        {
            this.icoBtn.show();
            return;
        }

        /* internal  */onBeaconModuleOpen(arg1: DataEvent): void
        {
            this.module.show();
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._view) 
            {
                this._view = new DomWarBeaModule();
            }
            return this._view;
        }

        /* internal  */get module(): DomWarBeaModule
        {
            return this.view as DomWarBeaModule;
        }

        private /* var */_icoBtn: DomWarBeaconHintBtn;

        private /* var */_auctionWin: AuctionPricePannel;

        private /* var */_auctionRank: AuctionRankPannel;

        private /* var */_inviteInfo: UnionInvoteInfoPannel;
    }
